//
//  MyMarkListViewController.m
//  Founder_study
//
//  Created by hongbao.cui on 14-8-21.
//  Copyright (c) 2014年 com.founder. All rights reserved.
//

#import "MyMarkListViewController.h"
#import "Sqlite.h"
#import "ApplicationUtils.h"
#import "MarkListTableViewCell.h"
#import "ResultLookViewController.h"
#import "FMNavgationItem.h"
@interface MyMarkListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataList;
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation MyMarkListViewController
@synthesize dataList;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FMSCREENWIDTH, [UIScreen mainScreen].bounds.size.height-20-44) style:UITableViewStylePlain];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [self.view addSubview:_tableView];
}
-(void)initData{
    NSArray *array = [Sqlite selectAllPaper:1];//选择已经答完题的试卷
    FMSCHBLog(@"=====array ====== %@",array);
    if (!self.dataList) {
        self.dataList = [NSMutableArray arrayWithCapacity:1];
    }
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"exam_time" ascending:NO];
    NSArray *dataArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
    [self.dataList addObjectsFromArray:dataArray];
}
-(void)back{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)initBack{
    FMNavgationItem* leftItem = [[FMNavgationItem alloc] init];
    [leftItem.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItems = [leftItem getNavgationItemWithType:WRNavgationTypeLeft Ima:[ApplicationUtils getLocalImage:@"Navigation-Bar_Return_btn_@2x"] ImaHighlight:[ApplicationUtils getLocalImage:@"Navigation-Bar_Return_btn_@2x"] title:@""];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.customTitle = @"我的成绩";
    [self initTableView];
    [self initBack];
    [self initData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    if (IS_STUDY) {
//        
//    }else{
//        self.navigationController.navigationBarHidden = NO;
//    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    if (IS_STUDY) {
//        
//    }else{
//        self.navigationController.navigationBarHidden = YES;
//    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    _tableView = nil;
    self.dataList = nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if ([self.dataList count]>0) {
        return [self.dataList count];
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([self.dataList count]>0) {
        static NSString *CellIdentifier = @"MarkListCell";
        MarkListTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        NSInteger row = indexPath.row;
        if (cell == nil) {
            cell = [[MarkListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        NSDictionary *dataDict = [self.dataList objectAtIndex:row];
        [cell setData:dataDict];
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        return cell;
    }else{
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.textLabel.text = @"暂无数据";
            cell.textLabel.font = [UIFont systemFontOfSize:20.0];
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        }
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    //    UILabel *label = (UILabel *)[cell viewWithTag:100];
    //    int w= label.frame.size.width;
    //    int h= label.frame.size.height;
    //    int y =(cell.frame.size.height -label.frame.size.height)/2;
    //    FMSCHBLog(@"cell.frame.size.height:%f",cell.frame.size.height);
    //    FMSCHBLog(@"label.frame.size.height:%f",label.frame.size.height);
    //    CGRect rect = CGRectOffset(CGRectMake(0, y, w, h), 2, 2);
    //    label.frame = rect;
    //    return label.frame.size.height;
    //    NSDictionary *dataDict = [self.dataList objectAtIndex:indexPath.row];
    //    NSString *text = [dataDict objectForKey:@"name"];
    //    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    //    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    //    CGFloat height = MAX(size.height, 50.0f);
    //    return height + (CELL_CONTENT_MARGIN * 2);
    return 63;
}

// Override to support conditional editing of the table view.
// - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
// {
// // Return NO if you do not want the specified item to be editable.
//     return YES;
// }
//

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dataDict = [self.dataList objectAtIndex:indexPath.row];
    NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithCapacity:1];
    [newDict setObject:[dataDict objectForKey:@"paper_id"] forKey:@"paperID"];
    [newDict setObject:[dataDict objectForKey:@"paper_title"] forKey:@"name"];
    [newDict setObject:[dataDict objectForKey:@"exam_time"] forKey:@"exam_time"];
    ResultLookViewController *synExercisesViewController = [[ResultLookViewController alloc] initWithNibName:@"ResultLookViewController" bundle:nil];
    synExercisesViewController.isMark = YES;
    synExercisesViewController.inforDict = [NSDictionary dictionaryWithDictionary:newDict];
    [self.navigationController pushViewController:synExercisesViewController animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
